package com.marvels.approve.common.task;

import java.util.List;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.marvels.approve.mqueues.dto.LoanApplyQueueDTO;
import com.marvels.approve.mqueues.service.ApproveILoanApplyMqReciever;
import com.marvels.approve.mqueues.service.ApproveILoanApplyQueueService;
import com.marvels.common.utils.MarvelsLogUtil;

@Component
public class loanApplyQueueScheduled implements Tasklet{

	@Autowired
	ApproveILoanApplyQueueService loanApplyQueueService;
	@Autowired
	ApproveILoanApplyMqReciever  loanApplyMqReciever;
	
	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext){
		MarvelsLogUtil.getInstance().info("开始处理没有成功处理完的进件单");
		LoanApplyQueueDTO loanApplyQueue = new LoanApplyQueueDTO();
		loanApplyQueue.setStatus(0);
		loanApplyQueue.setDeleteFlag(0);
		loanApplyQueue.setPageSize(10000);
		List<LoanApplyQueueDTO> loanApplyQueueList = loanApplyQueueService.selectExpiredLoanApplyQueue(loanApplyQueue);
		MarvelsLogUtil.getInstance().info("获取超时未处理的单list size="+loanApplyQueueList.size());
		for(LoanApplyQueueDTO loanApplyQueueDTO : loanApplyQueueList) {
			MarvelsLogUtil.getInstance().info("重新发送进件单号,loanid="+loanApplyQueueDTO.getLoanId());
			try {
				loanApplyMqReciever.process(loanApplyQueueDTO.getLoanId());
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error("处理进件单失败", e);
			}
		}
		MarvelsLogUtil.getInstance().info("完成处理没有成功处理完的进件单,处理完数量,"+loanApplyQueueList.size());
		return RepeatStatus.FINISHED;
	}
}
